package com.winz.cultureapp.activity;

import android.content.Intent;
import android.graphics.Color;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.vp.NewsVpAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.entity.EventBusBean;
import com.winz.cultureapp.entity.LegacyTypeBean;
import com.winz.cultureapp.filter.intangible.IntangiblePopWindow;
import com.winz.cultureapp.filter.intangible.PublicationPopWindow;
import com.winz.cultureapp.fragment.ContactUsFragment;
import com.winz.cultureapp.fragment.ProjectFragment;
import com.winz.cultureapp.fragment.PublicationFragment;
import com.winz.cultureapp.fragment.SummaryFragment;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文化非遗
 */
public class IntangibleActivity extends BaseAppCompatActivity implements ViewPager.OnPageChangeListener, IntangiblePopWindow.OnFinishListener, PublicationPopWindow.OnFinishListener {
    public final static int PROJECT_FLAG = 0x0001;
    public final static int GAIN_FLAG = 0x0002;

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.stb_intangible)
    SlidingTabLayout mStb_intangible;
    @BindView(R.id.vp_intangible)
    ViewPager mVp_intangible;
    @BindView(R.id.tv_pic)
    TextView mTv_right;
    @BindView(R.id.iv_right)
    ImageView mIv_right;
    @BindView(R.id.rl_right)
    RelativeLayout mRl_right;

    private String[] titles; //{"非遗概述", "联系我们"}
    private List<Fragment> fragments = new ArrayList<>();

    private int position = 0; // 当前viewpage位置


    private PublicationPopWindow mPop_public;
    private NewsVpAdapter mAdapter;
    private String researchCategoryId = "0";
    private String categoryName = "全部";

    @Override
    protected int contentView() {
        return R.layout.activity_intangible;
    }

    @Override
    protected void initViews() {
        setStatusBar(Color.WHITE);
        mTv_top_title.setText(R.string.home_item5_title);
        mIv_right.setImageResource(R.drawable.icon_right_xx);
        mTv_right.setText("全部");
        mRl_right.setVisibility(View.GONE);


//        fragments.add(ProjectFragment.getInstance());
//        fragments.add(SummaryFragment.getInstance());
//        fragments.add(IntangibleFragment.getInstance());
//        fragments.add(PublicationFragment.getInstance());
//        fragments.add(ContactUsFragment.getInstance());


        // 初始化
        mPop_public = new PublicationPopWindow(this,GAIN_FLAG);
        mPop_public.setOnFinishListener(this);
    }

    @Override
    protected void loadDatas(Intent intent) {
        //获取所有文化非遗类别列表
        getlegacyType();
    }

    /**
     * 获取所有文化非遗类别列表
     */
    private void getlegacyType() {
        showProgressDialog();
        HttpUitls.onGet("legacy_category",new OnResponeListener<LegacyTypeBean>() {
            @Override
            public void onSuccess(LegacyTypeBean bean) {
                Logger.d("legacy_category==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    // 可以初始化导航
                    initTitles(bean.data);
                    // 初始化界面
                    initFragments(bean.data);

                    mAdapter = new NewsVpAdapter(getSupportFragmentManager(), fragments, titles);
                    mVp_intangible.setAdapter(mAdapter);

                    mStb_intangible.setViewPager(mVp_intangible, titles);
                    mVp_intangible.setOffscreenPageLimit(titles.length);
                    mVp_intangible.addOnPageChangeListener(IntangibleActivity.this);
                }else{
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
            }
        });
    }

    /**
     * 初始化导航标题
     * @param data
     */
    private void initTitles(LegacyTypeBean.DataBean data) {
        if (data.list.size()>0){
            titles = new String[data.list.size() + 3];

            for (int i=0;i<data.list.size();i++){
                titles[i] = data.list.get(i).categoryName;
            }
            titles[titles.length-3] = "研究成果及多媒体出版物";

            titles[titles.length-2] = "非遗概述";
            titles[titles.length-1] = "联系我们";
        }
    }

    /**
     * 初始化界面
     */
    private void initFragments(LegacyTypeBean.DataBean data) {
        // 添加动态部分
        if (data.list.size()>0){
            for (int i=0;i<data.list.size();i++){
                fragments.add(ProjectFragment.getInstance(data.list.get(i).legacyCategoryId));
            }
        }

        // 研究成果及多媒体出版物
        fragments.add(PublicationFragment.getInstance());
        // 非遗概述
        fragments.add(SummaryFragment.getInstance());
        // 联系我们
        fragments.add(ContactUsFragment.getInstance());

    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        this.position = position;
        changeState(position);

    }

    /**
     * 切换状态
     * @param position
     */
    private void changeState(int position) {
        if (position == 2){
            mRl_right.setVisibility(View.VISIBLE);
            mIv_right.setVisibility(View.VISIBLE);
            mTv_right.setVisibility(View.VISIBLE);
            mTv_right.setText(categoryName);
        }else {
            mRl_right.setVisibility(View.GONE);
            mIv_right.setVisibility(View.GONE);
            mTv_right.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @OnClick({R.id.fl_back,R.id.rl_right,R.id.tv_pic})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.tv_pic:
            case R.id.rl_right:
                // 弹出筛选框
                mPop_public.showAsDropDown(view);
                break;
        }
    }


    @Override
    public void onFinish(int flag, String data) {
        categoryName = data.split(",")[0];
        researchCategoryId = data.split(",")[1];
        mTv_right.setText(categoryName);

        // 通知刷新页面
        EventBusBean<String> eventBusBean = new EventBusBean<>(flag-1,0,researchCategoryId);
        EventBus.getDefault().post(eventBusBean);

    }

}
